home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap09 / BtnLook / BtnLook.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.1 KB  |  148 lines

  1. /*----------------------------------------
  2.    BTNLOOK.C -- Button Look Program
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. struct
  9. {
  10.      int     iStyle ;
  11.      TCHAR * szText ;
  12. }
  13. button[] =
  14. {
  15.      BS_PUSHBUTTON,      TEXT ("PUSHBUTTON"),
  16.      BS_DEFPUSHBUTTON,   TEXT ("DEFPUSHBUTTON"),
  17.      BS_CHECKBOX,        TEXT ("CHECKBOX"), 
  18.      BS_AUTOCHECKBOX,    TEXT ("AUTOCHECKBOX"),
  19.      BS_RADIOBUTTON,     TEXT ("RADIOBUTTON"),
  20.      BS_3STATE,          TEXT ("3STATE"),
  21.      BS_AUTO3STATE,      TEXT ("AUTO3STATE"),
  22.      BS_GROUPBOX,        TEXT ("GROUPBOX"),
  23.      BS_AUTORADIOBUTTON, TEXT ("AUTORADIO"),
  24.      BS_OWNERDRAW,       TEXT ("OWNERDRAW")
  25. } ;
  26.  
  27. #define NUM (sizeof button / sizeof button[0])
  28.  
  29. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  30.  
  31. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  32.                     PSTR szCmdLine, int iCmdShow)
  33. {
  34.      static TCHAR szAppName[] = TEXT ("BtnLook") ;
  35.      HWND         hwnd ;
  36.      MSG          msg ;
  37.      WNDCLASS     wndclass ;
  38.      
  39.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  40.      wndclass.lpfnWndProc   = WndProc ;
  41.      wndclass.cbClsExtra    = 0 ;
  42.      wndclass.cbWndExtra    = 0 ;
  43.      wndclass.hInstance     = hInstance ;
  44.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  45.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  46.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  47.      wndclass.lpszMenuName  = NULL ;
  48.      wndclass.lpszClassName = szAppName ;
  49.      
  50.      if (!RegisterClass (&wndclass))
  51.      {
  52.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  53.                       szAppName, MB_ICONERROR) ;
  54.           return 0 ;
  55.      }
  56.      
  57.      hwnd = CreateWindow (szAppName, TEXT ("Button Look"),
  58.                           WS_OVERLAPPEDWINDOW,
  59.                           CW_USEDEFAULT, CW_USEDEFAULT,
  60.                           CW_USEDEFAULT, CW_USEDEFAULT,
  61.                           NULL, NULL, hInstance, NULL) ;
  62.      
  63.      ShowWindow (hwnd, iCmdShow) ;
  64.      UpdateWindow (hwnd) ;
  65.      
  66.      while (GetMessage (&msg, NULL, 0, 0))
  67.      {
  68.           TranslateMessage (&msg) ;
  69.           DispatchMessage (&msg) ;
  70.      }
  71.      return msg.wParam ;
  72. }
  73.  
  74. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  75. {
  76.      static HWND  hwndButton[NUM] ;
  77.      static RECT  rect ;
  78.      static TCHAR szTop[]    = TEXT ("message            wParam       lParam"),
  79.                   szUnd[]    = TEXT ("_______            ______       ______"),
  80.                   szFormat[] = TEXT ("%-16s%04X-%04X    %04X-%04X"),
  81.                   szBuffer[50] ;
  82.      static int   cxChar, cyChar ;
  83.      HDC          hdc ;
  84.      PAINTSTRUCT  ps ;
  85.      int          i ;
  86.      
  87.      switch (message)
  88.      {
  89.      case WM_CREATE :
  90.           cxChar = LOWORD (GetDialogBaseUnits ()) ;
  91.           cyChar = HIWORD (GetDialogBaseUnits ()) ;
  92.           
  93.           for (i = 0 ; i < NUM ; i++)
  94.                hwndButton[i] = CreateWindow ( TEXT("button"), 
  95.                                    button[i].szText,
  96.                                    WS_CHILD | WS_VISIBLE | button[i].iStyle,
  97.                                    cxChar, cyChar * (1 + 2 * i),
  98.                                    20 * cxChar, 7 * cyChar / 4,
  99.                                    hwnd, (HMENU) i,
  100.                                    ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
  101.           return 0 ;
  102.  
  103.      case WM_SIZE :
  104.           rect.left   = 24 * cxChar ;
  105.           rect.top    =  2 * cyChar ;
  106.           rect.right  = LOWORD (lParam) ;
  107.           rect.bottom = HIWORD (lParam) ;
  108.           return 0 ;
  109.           
  110.      case WM_PAINT :
  111.           InvalidateRect (hwnd, &rect, TRUE) ;
  112.           
  113.           hdc = BeginPaint (hwnd, &ps) ;
  114.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  115.           SetBkMode (hdc, TRANSPARENT) ;
  116.           
  117.           TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop)) ;
  118.           TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd)) ;
  119.           
  120.           EndPaint (hwnd, &ps) ;
  121.           return 0 ;
  122.           
  123.      case WM_DRAWITEM :
  124.      case WM_COMMAND :
  125.           ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
  126.           
  127.           hdc = GetDC (hwnd) ;
  128.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  129.           
  130.           TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
  131.                    szBuffer,
  132.                    wsprintf (szBuffer, szFormat,
  133.                          message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM") : 
  134.                                                   TEXT ("WM_COMMAND"),
  135.                          HIWORD (wParam), LOWORD (wParam),
  136.                          HIWORD (lParam), LOWORD (lParam))) ;
  137.           
  138.           ReleaseDC (hwnd, hdc) ;
  139.           ValidateRect (hwnd, &rect) ;
  140.           break ;
  141.           
  142.      case WM_DESTROY :
  143.           PostQuitMessage (0) ;
  144.           return 0 ;
  145.      }
  146.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  147. }
  148.